package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.constants.MQConstants;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.*;
import com.leyou.item.mapper.SpuMapper;
import com.leyou.item.service.*;
import com.netflix.eureka.Names;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import static com.leyou.common.constants.MQConstants.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailService detailService;
    @Autowired
    private SpuService spuService;
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 分页查询spu
     * @param brandId
     * @param categoryId
     * @param id
     * @param page
     * @param rows
     * @param saleable
     * @return
     */
    @Override
    public PageDTO<SpuDTO> querySpuByPage(Long brandId, Long categoryId, Long id, Integer page, Integer rows, Boolean saleable) {
        //健壮性
        int current =Math.max(page,1);
        int size =Math.max(rows,5);
        //准备查询条件SELECT * FROM tb_spu WHERE saleable = ? AND category_id = ? AND brand_id = ? AND id = ?
        Page<Spu> result = query().eq(saleable !=null,"saleable",saleable)
                                  .eq(categoryId != null, "cid3", categoryId)
                                  .eq(brandId != null, "brand_id", brandId)
                                  .eq(id != null, "id", id)
                                    // 3.准备分页条件 LIMIT ?, ?
                                 .page(new Page<>(current, size));
        //解析查询结果
        long total = result.getTotal();
        long pages = result.getPages();
        List<Spu> list = result.getRecords();
        List<SpuDTO> dtoList = SpuDTO.convertEntityList(list);
        for (SpuDTO spuDTO : dtoList) {
            //查询spu的分类和品牌的名称
            handleCategoryAndBrandName(spuDTO);
        }
        //封装分页结果并返回
        return new PageDTO<>(total,pages,dtoList);
    }

    /**
     * 新增商品
     * spu增 spudetail增 sku也得增
     * @param spuDTO
     */
    @Transactional
    @Override
    public void saveGoods(SpuDTO spuDTO) {
        //新增Spu
        Spu spu = spuDTO.toEntity(Spu.class);
        spu.setSaleable(false);
        //判断是否添加成功 保存回显
        boolean success = save(spu);
        if (!success){
            throw new LyException(500,"新增商品失败");
        }
        //新增SpuDetail
        SpuDetail spuDetail = spuDTO.getSpuDetail().toEntity(SpuDetail.class);
        spuDetail.setSpuId(spu.getId());
        if (!success){
            throw new LyException(500,"新增商品详情失败");
        }
        //新增Sku
        List<Sku>list=spuDTO.getSkus().stream().map(skuDTO -> {
            Sku sku = skuDTO.toEntity(Sku.class);
            sku.setSaleable(false);
            sku.setSpuId(spu.getId());
            return sku;
        }).collect(Collectors.toList());
        //批量新增
        skuService.saveBatch(list);
    }

    /**
     * 修改商品上下架
     * SPU下架时，其关联的SKU也必须保持下架状态，
     * 因此这里业务中不仅更新了SPU表，也更新了SKU表。
     * @param id
     * @param saleable
     */
    @Override
    @Transactional
    public void updateSaleable(Long id, Boolean saleable) {

        //更新Spu
        Spu spu = new Spu();
        spu.setId(id);
        spu.setSaleable(saleable);

        boolean success = updateById(spu);
        if (!success){
            throw new LyException(500,"更新失败");
        }
        //更新Sku
        success=skuService.update().eq("spu_id",id).set("saleable",saleable).update();
        if (!success){
            throw new LyException(500,"更新失败");
        }
        //发送MQ消息
        String routingKey = saleable ? MQConstants.RoutingKeyConstants
                .ITEM_UP_KEY: MQConstants.RoutingKeyConstants
                .ITEM_DOWN_KEY;
        amqpTemplate.convertAndSend(ExchangeConstants.ITEM_EXCHANGE_NAME,routingKey,id);
    }

    @Override
    public SpuDTO queryGoodsById(Long id) {
        //查询spu
        //1.1查询
        Spu spu = getById(id);
        if (spu == null){
            throw new LyException(400,"商品id不存在");
        }
        //1.2转换DTO
        SpuDTO spuDTO = new SpuDTO(spu);
        //查询spuDetail
        SpuDetail detail = detailService.getById(id);
        if (detail== null){
            throw new LyException(400,"商品id不存在");
        }
        spuDTO.setSpuDetail(new SpuDetailDTO(detail));
        //查询sku
        List<Sku> list = skuService.query().eq("spu_id", id).list();
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(400,"商品id不存在");
        }
        spuDTO.setSkus(SkuDTO.convertEntityList(list));

        //准备商品分类和品牌名称
        handleCategoryAndBrandName(spuDTO);
        return spuDTO;
    }

    @Override
    @Transactional
    public void updateGoods(SpuDTO spuDTO) {
        //修改Spu
        //判断是否存在spu的id，有说明需要修改，没有说明不需要
        Long spuId = spuDTO.getId();
        if (spuId !=null){
            //1.2spu需要修改，更新spu, 转换dto
            Spu spu = spuDTO.toEntity(Spu.class);
            //1.3更新
            boolean success = updateById(spu);
            if (!success){
                //更新失败 抛出异常
                throw new LyException(500, "更新商品失败！");
            }
        }
        //修改spuDetail
        SpuDetailDTO detailDTO = spuDTO.getSpuDetail();
        //判断是否为null
        if(detailDTO !=null && detailDTO.getSpuId()!=null){
            //spuDetail存在 需要修改，转换DTO
            SpuDetail detail = detailDTO.toEntity(SpuDetail.class);
            boolean success = detailService.updateById(detail);
            if (!success){
                //更新失败
                throw new LyException(500, "更新商品失败！");

            }
        }
        //修改sku
        List<SkuDTO> dtoList = spuDTO.getSkus();
        //判断是否包含sku
        if(CollectionUtils.isEmpty(dtoList)){
            return;
        }
        // 3.2.转换DTO，并将sku根据saleable是否为null来分组。
        // null，是新增或修改，不是null是删除
        Map<Boolean,List<Sku>>map=dtoList.stream()
                .map(skuDTO -> skuDTO.toEntity(Sku.class))
                .collect(Collectors.groupingBy(sku ->
                        sku.getSaleable()==null));
        //获取要新增或修改的sku
        List<Sku> insertOrUpdateList = map.get(true);
        //如果不为空 则修海或新增
        if(CollectionUtils.isEmpty(insertOrUpdateList)){
            skuService.saveOrUpdateBatch(insertOrUpdateList);
        }
        //获取要删除的sku
        List<Sku> deleteSkuList = map.get(false);
        if (!CollectionUtils.isEmpty(deleteSkuList)){
            List<Long> idLisst = deleteSkuList.stream().map(Sku::getId).collect(Collectors.toList());
            //删除
            skuService.removeByIds(idLisst);
        }
    }

    /**
     * 根据spuId查询商品Spu
     * @param id
     * @return
     */

    @Override
    public SpuDTO querySpuById(Long id) {

        return new SpuDTO(getById(id));
    }

    private void handleCategoryAndBrandName(SpuDTO spuDTO) {

        //根据品牌id查新品牌名称
        Brand brand = brandService.getById(spuDTO.getBrandId());
        if (brand !=null){
            spuDTO.setBrandName(brand.getName());
        }
        //根据三级分类id 查询分类集合
        List<Category> categories = categoryService.listByIds(spuDTO.getCategoryIds());
        //取出分类的名称 拼接起来
        String names = categories.stream().map(Category::getName).collect(Collectors.joining("/"));
        spuDTO.setCategoryName(names);
    }
}
